package com.imyuanma.qingyun.ums.controller;

import com.imyuanma.qingyun.common.core.structure.tree.TreeBuilder;
import com.imyuanma.qingyun.common.model.response.Result;
import com.imyuanma.qingyun.common.util.CollectionUtil;
import com.imyuanma.qingyun.interfaces.common.model.enums.ETrashFlagEnum;
import com.imyuanma.qingyun.interfaces.monitor.annotation.Trace;
import com.imyuanma.qingyun.ums.model.UmsMenu;
import com.imyuanma.qingyun.ums.model.UmsResource;
import com.imyuanma.qingyun.ums.model.enums.EUmsResourceStatusEnum;
import com.imyuanma.qingyun.ums.model.enums.EUmsResourceTypeEnum;
import com.imyuanma.qingyun.ums.service.IUmsResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单web
 *
 * @author wangjy
 * @date 2022/10/05 21:01:57
 */
@RestController
@RequestMapping(value = "/ums/menu")
public class UmsMenuController {

    @Autowired
    private IUmsResourceService umsResourceService;

    @Trace("查询菜单")
    @PostMapping("/getMenuTree")
    public Result<List<UmsMenu>> getMenuTree() {
        UmsResource query = new UmsResource();
        query.setType(EUmsResourceTypeEnum.CODE_10.getCode());
        query.setStatus(EUmsResourceStatusEnum.VALID.getCode());
        query.setTrashFlag(ETrashFlagEnum.VALID.getType());
        // 查询资源列表
        List<UmsResource> resourceList = umsResourceService.getList(query);
        // 转菜单树
        if (CollectionUtil.isNotEmpty(resourceList)) {
            return Result.success(TreeBuilder.buildTree(resourceList.stream().map(UmsMenu::convertFrom).sorted().collect(Collectors.toList())));
        }
        return Result.success();
    }

}
